x=imread('flower2.jpg');
figure, imshow(x,[]);
[M N]=size(x);
%median filter: 3*3 non-linear filter
for a=2:M-1
for b=2:N-1
A=[x(a-1,b-1) x(a,b-1) x(a-1,b) x(a+1,b) x(a,b+1) x(a+1,b+1) x(a-1,b+1) x(a+1,b-1) ];
x(a,b)=median(A);
end
end

figure , imshow(x,[]);



%The filter is effective in removing the noise to a great extent , however it comes at the cost of sharpness . Due to the median filter the clarity of the 
%image is lost . As you take more and more neighbouring pixels the image will become more blurred .

y=imread('flower3.jpg');
figure, imshow(y,[]);
[M N]=size(y);
for a=2:M-1
for b=2:N-1
A=[x(a,b-1) x(a-1,b) x(a+1,b) x(a,b+1)];
y(a,b)=median(A);
end
end

figure , imshow(y,[]);
